/*
* @Author: steven.wang
* @Date:   2019-01-10 11:20:47
* @Last Modified by:   steven.wang
* @Last Modified time: 2019-05-13 14:45:04
*/
const db = require('../db');
/**
 * 根据条件查询图片列表
 * @author steven.wang
 * @DateTime 2019-02-26T16:30:01+0800
 * @param    {Number}                 id 图片ID
 * @return   {Promise}
 */
module.exports.list = values => {
	const dbSql = db.generateSql({
		wants: 'id, type, fr, name, url, thumbnail, status',
		action: 'search',
		table: 'photo',
		conditions: {
			status: [values.status === '' && ''],
			type: [values.type || ''],
			fr: [values.fr || '']
		},
		likes: {
			name: values.name
		},
		sorts: values.sorts,
		pages: {
			page: parseInt(values.page) || 1,
			pageSize: parseInt(values.pageSize) || 10
		}
	});
	return db.query(dbSql.sql, dbSql.sqlData);
}
/**
 * 获取图片总数
 * @author steven.wang
 * @DateTime 2019-03-01T16:26:50+0800
 */
module.exports.count = values => {
	const dbSql = db.generateSql({
		wants: 'count(*)',
		action: 'search',
		table: 'photo',
		conditions: {
			status: [values.status === '' && ''],
			type: [values.type || ''],
			fr: [values.fr || '']
		},
		likes: {
			name: values.name
		}
	})
	return db.query(dbSql.sql, dbSql.sqlData);
}
/**
 * 添加图片
 * @author steven.wang
 * @DateTime 2019-02-26T16:29:26+0800
 * @param    {Array}                 value 图片信息
 * @return   {Promise}
 */
module.exports.add = values => {
	const dbSql = db.generateSql({
		action: 'add',
		table: 'photo',
		conditions: [values]
	});
	return db.query(dbSql.sql, dbSql.sqlData);
}

/**
 * 删除图片
 * @author steven.wang
 * @DateTime 2019-02-26T16:30:37+0800
 * @param    {Number}                 id 图片ID
 * @return   {Promise}
 */
module.exports.delete = values => {
	const dbSql = db.generateSql({
		action: 'delete',
		table: 'photo',
		conditions: {
			id: values.ids
		}
	});
	return db.query(dbSql.sql, dbSql.sqlData);
}

/**
 * 更新图片
 * @author steven.wang
 * @DateTime 2019-02-26T16:30:58+0800
 * @param    {Array}                 value
 * @return   {Promise}
 */
module.exports.update = values => {
	let id = values.id;
	delete values.id;
	delete values.status;
	const dbSql = db.generateSql({
		action: 'update',
		table: 'photo',
		wants: values,
		conditions: {
			id: id
		}
	});
	return db.query(dbSql.sql, dbSql.sqlData);
}
